﻿using System.Collections;
using System.Collections.Generic;
using Dragon.Library.Framework.Entity.Search.Page;
using Dragon.Library.Framework.Entity.Search.Query;

namespace Dragon.Library.Framework.Bo
{
    public partial class BaseBo<T, TBean, TKey>
    {
        protected IList<TBean> Select(QueryGroup queryGroup, QueryLevel queryLevel)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            var tBeans = Dao.Select(queryGroup, queryLevel);
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            DisposeDao();
            DisposeSession();
            //IList<TBean> newTb = new List<TBean>();
            //foreach (var tBean in tBeans)
            //{
            //    newTb.Add((TBean)tBean.Clone());
            //}
            return tBeans;
        }

        protected IList<TBean> Select(QueryString queryString)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            var tBeans = Dao.Select(queryString);
            DisposeDao();
            DisposeSession();
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            //IList<TBean> newTb = new List<TBean>();
            //foreach (var tBean in tBeans)
            //{
            //    newTb.Add((TBean)tBean.Clone());
            //}
            return tBeans;
        }

        protected IList SelectList(QueryString queryString)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            var tBeans = Dao.SelectList(queryString);
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            DisposeDao();
            DisposeSession();
            return tBeans;
        }

        protected PageResult<TBean> GetPageList(PageSearch pageSearch, QueryLevel queryLevel)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            var tBeans = Dao.GetPageList(pageSearch, queryLevel);
            DisposeDao();
            DisposeSession();
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            //IList<TBean> newTb = new List<TBean>();
            //foreach (var tBean in tBeans.Data)
            //{
            //    newTb.Add((TBean)tBean.Clone());
            //}
            //tBeans.Data = newTb;
            return tBeans;
        }

        protected PageResult<TBean> GetPageList(PageSearchString pageSearch)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            var tBeans = Dao.GetPageList(pageSearch);
            DisposeDao();
            DisposeSession();
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            //IList<TBean> newTb = new List<TBean>();
            //foreach (var tBean in tBeans.Data)
            //{
            //    newTb.Add((TBean)tBean.Clone());
            //}
            //tBeans.Data = newTb;
            return tBeans;
        }

        protected TBean GetObjectByKey(TKey key)
        {
            //if (!Dao.IsOpen)
            //    Dao.ConnectionOpen();
            InitializationSession();
            InitializationDao();
            var tBeans = Dao.GetObjectByKey(key);
            DisposeDao();
            DisposeSession();
            //if (Dao.IsOpen && !Dao.IsTran)
            //    Dao.ConnectionClose();
            return tBeans;
        }
    }
}
